//

//  KSBaseViewController.swift
//  Supermarket
//
//  Created by liuxm on 2017/8/21.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

class KSBaseViewController: UIViewController,KSRequestDelegate  {
    /// 网络请求
    let request = KSRequest()
    // 提示消息
    lazy var progressHUD: KSProgressHUD = {
        let hud = KSProgressHUD(frame: (KSConstants.kWindow?.frame)!)
        return hud
    }()
    
    /// 加载
    lazy var loadingImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "Common_Loading"))
        imageView.frame = CGRect(x: (KSConstants.kScreenWidth - 30)/2.0, y: (KSConstants.kScreenHeight - 30)/2.0 - 64, width: 30, height: 30)
        
        self.view.addSubview(imageView)
        return imageView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.view.backgroundColor = KSColor.kColorViewBackground
        
        self.request.delegate = self
    }
    
    override func viewDidAppear(_ animated: Bool) {
        // 进入页面百度页面统计
        BaiduMobStat.default().pageviewStart(withName: self.title)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        // 退出页面百度页面统计
        BaiduMobStat.default().pageviewEnd(withName: self.title)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    /// MARK: - KSRequestDelegate
    func requestSuccess(result: String, apiType: KSApiType) {
        self.endLoading()
    }
    
    func requestFaile(code: Int, message: String, apiType: KSApiType) {
        self.endLoading()
        // token失效
        if code == 401 {
            self.skipLoginPage()
        } else {
            progressHUD.showMessage(text: message, duration: 2, animated: true)
        }
    }
    
    /// token失效，跳转登录页面
    func skipLoginPage() {
        let alertVC = UIAlertController(title: "提示", message: "Token过期失效，请重新登录", preferredStyle: UIAlertControllerStyle.alert)
        let acSure = UIAlertAction(title: "确定", style: .default) { (UIAlertAction) -> Void in
            KSTools.exitUpdateLoginInfo()
            
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.switchLoginPage()
        }
        alertVC.addAction(acSure)
        self.present(alertVC, animated: true, completion: nil)
    }
    
    /// 开始加载动画
    func startLoading() {
        self.view.isUserInteractionEnabled = false
        self.loadingImageView.isHidden = false
        let animation = CABasicAnimation(keyPath: "transform.rotation.z")
        animation.toValue = (360.0 * .pi) / 180.0
        animation.duration = 2
        animation.repeatCount = 999999
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        self.loadingImageView.layer.add(animation, forKey: "revItUpAnimation")
    }
    
    /// 结束加载动画
    func endLoading() {
        self.view.isUserInteractionEnabled = true
        self.loadingImageView.isHidden = true
    }
    
    deinit {
        print("释放内存", self.description)
    }
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
